import render from "./C_render.js";
// 功能是：购物车内数量的加减，商品的删除，以及金额的计算
function ChengeNum(){}
$.extend(ChengeNum.prototype,{
    init : function(){
        this.wrapper = $(".shows tbody");
        this.wrapper.on("click",".c_add",$.proxy(function(){
            this.changeNum("add")
        },this))
        this.wrapper.on("click",".c_reduce",$.proxy(function(){
            this.changeNum("reduce")
        },this))
        this.wrapper.on("change",".c_num",$.proxy(function(){
            this.changeNum("change")
        },this))
        this.wrapper.on("click",".removess",$.proxy(function(){
            this.remove()
        },this))
        this.wrapper.on("click",".c_item a",$.proxy(function(){
            this.chengeClass()
        },this))
    },
    // 数量的改变
    changeNum : function(type,evt){
        var e = evt || window.event;
        var target = e.target || e.srcElement;
        var nums = null;
        if(type == "add" || type == "reduce"){
            this.text = $(target).siblings(".c_num")
            nums = (type == "add" ? parseInt(this.text.val()) + 1 : parseInt(this.text.val()) - 1);
            nums = nums <= 1 ? 1 : nums;
        }else{
            this.text = $(target);
            nums = parseInt(this.text.val())
            nums = nums <= 1 ? 1 : nums;
        }
        this.text.val(nums);
        var id = this.text.attr("data-id")
        var list = localStorage.getItem("carts")
        list = JSON.parse(list);
        $.each(list,$.proxy(function(index,item){
            if(item.id === id){
                 item.count = this.text.val();
            }
        },this))
        localStorage.setItem("carts",JSON.stringify(list));
        this.sumPrice(this.text.val())
    },
    //单个商品的总价
    sumPrice :function(num,evt){
        var e = evt || window.event;
        var target = e.target || e.srcElement;
        var sum_price_wraper = $(target).parents(".numss").siblings(".sumss")
        var one_price_wraper = $(target).parents(".numss").siblings(".one_price").children(".c_price");
        var sum = parseInt(one_price_wraper.html())*num
        sum_price_wraper.html(`${sum}.00`);    
    },
    // 删除一个商品
    remove : function(evt){
        var e = evt || window.event;
        var target = e.target || e.srcElement;
        var ids = parseInt($(target).attr("data-id"));
        var wrapper=$(target).parents("tr");
        var list = localStorage.getItem("carts")
        list = JSON.parse(list);
        
        for(var i = 0 , item ; item = list[i];i++){
           if(item.id == ids){
               list.splice(i,1)
               localStorage.setItem("carts",JSON.stringify(list))
               wrapper.remove();
               if(list.length == 0){
                   console.log(1);
                   localStorage.removeItem("carts");
                   location.reload(true);
               }
               break;
           }
        }
    },
    // 商品的选中 总金额的计算
    chengeClass:function(evt){
        var e = evt || window.event;
        var target = e.target || e.srcElement;
        $(target).hasClass("active")  ? $(target).removeClass("active") : $(target).addClass("active") ;
        var list = $(".active");
        var total = null;
        $.each(list,$.proxy(function(index,item){
           var sum = $(item).parent().siblings(".sumss").html()
           total += Number(sum);
        },this))
        total = total == null ? 0 : total;
       $(".SUM").html(`商品总价：${total}.00`);
       $(".totals").html(`${total}.00`)
    }
})
export default new ChengeNum();